package org.hxyjs.controller;

import org.hxyjs.dto.DTO;
import org.hxyjs.entity.MatchType;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.MatchTypeService;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@Controller //@Controller注解是用spring容器生成 对象 默认对象名 matchTypeController
//@RestController //@RequestMapping 用户请求查找该资源使用的名字 网名 默认  /matchTypeController
public class MatchTypeController {
	@Resource
    MatchTypeService matchTypeService;
	@GetMapping("/MatchTypes")
	@ResponseBody //把方法的返回值按json转换之后 输出到浏览器
    public DTO<MatchType> getAllMatchType() throws Exception{
    	System.out.println("查询全部比赛类型");
    	return matchTypeService.getAllMatchTypes();
    }

    @DeleteMapping("/MatchTypes/{matno}")
	@ResponseBody
    public DTO<MatchType> deleteMatchTypeByMatno(@RequestBody @PathVariable("matno") String matno) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
    	System.out.println(matno+"删除比赛类型");
    	return matchTypeService.deleteMatchTypeByMatno(matno);
    }

	@PostMapping("/MatchTypes")
	@ResponseBody
	public DTO<MatchType> addMatchType(@RequestBody MatchType matchType) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("增加比赛类型");
		return matchTypeService.addMatchType(matchType);
	}

	@PatchMapping("/MatchTypes")
	@ResponseBody
	public DTO<MatchType> updateMatchTypeByMatno(@RequestBody MatchType matchType) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("修改比赛类型成功");
		System.out.println(matchType);
		return matchTypeService.updateMatchTypeByMatno(matchType);
	}
	@GetMapping("/MatchTypes/{matno}")
	@ResponseBody
	public DTO<MatchType> getMatchTypeByMatno(@PathVariable("matno") String matno) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("查询比赛类型");
		return matchTypeService.getMatchTypeByMatno(matno);
	}
	@GetMapping("/MatchTypes/pageInfo")
	@ResponseBody
	public DTO<MatchType> getMatchTypeByPage(@SpringQueryMap PageInfo pageInfo) throws Exception{
		String cacheId = "MatchTypeByPage" + pageInfo.getCurrPage();//自编redis每页数据缓存的key
		return pageInfo.getCurrPage() > 2 ? matchTypeService.getMatchTypeByPage(pageInfo) :
				matchTypeService.getMatchTypeByPage(pageInfo,cacheId);
	}
//	@RequestMapping("/selectMatchTypeWithorderAndUser/{matno}")
//	@ResponseBody
//	public DTO<MatchType> selectMatchTypeWithorderAndUser(@PathVariable("matno")String matno) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
//		System.out.println("查询订单带上用户");
//		return matchTypeService.selectMatchTypeWithorderAndUser(matno);
//	}
}
